package com.my.ddd_sample_collections.aggregate_change_tracking.abstract_domain;


import com.my.ddd_sample_collections.aggregate_change_tracking.domain.ItemDO;
import com.my.ddd_sample_collections.aggregate_change_tracking.repository_framework.AggregateRootSupport;
import lombok.Builder;
import lombok.experimental.SuperBuilder;

import java.util.List;
import java.util.Objects;

@SuperBuilder
public abstract class AbstractMyAggregateDO extends AggregateRootSupport {
    
    private String foo;
    private String bar;
    private AbstractComplexDO complex;
    private AbstractSimpleDO simple;
    //此字段必须设置为final的，排除在builder之外，否则整个类无法使用lombok builder
    //此处不能使用泛型（原因比较复杂难理解） 参考：https://docs.oracle.com/javase/tutorial/java/generics/capture.html
    private final MyArrayList<AbstractItemDO> items = new MyArrayList<>(this, "items");
    private List<String> simpleItems;
    private String[] arrayItems;

    protected void setFoo(String foo) {
        if (!Objects.deepEquals(this.foo, foo)) {
            track(this, this.foo,"foo", foo);
        }
        this.foo = foo;
    }

    protected void setBar(String bar) {
        if (!Objects.deepEquals(this.bar, bar)) {
            track(this,this.complex, "bar", complex);
        }
        this.bar = bar;
    }

    protected void setComplex(AbstractComplexDO complex) {
        if (!Objects.deepEquals(this.complex, complex)) {
            track(this,this.complex, "complex", complex);
        }
        this.complex = complex;
    }

    protected void setSimple(AbstractSimpleDO simple) {
        if (!Objects.deepEquals(this.simple, simple)) {
            track(this, this.simple, "simple", simple);
        }
        this.simple = simple;
    }

//    protected void setItems(List<AbstractItemDO> items) {
//        if (!Objects.deepEquals(this.items, items)) {
//            track(this, this.items, "items", items);
//        }
//        this.items = items;
//    }

    protected void setSimpleItems(List<String> simpleItems) {
        if (!Objects.deepEquals(this.simpleItems, simpleItems)) {
            track(this, this.simpleItems, "simpleItems", simpleItems);
        }
        this.simpleItems = simpleItems;
    }

    protected void setArrayItems(String[] arrayItems) {
        if (!Objects.deepEquals(this.arrayItems, arrayItems)) {
            track(this, this.arrayItems, "arrayItems", arrayItems);
        }
        this.arrayItems = arrayItems;
    }

    public String getFoo() {
        return foo;
    }

    public String getBar() {
        return bar;
    }

    public AbstractComplexDO getComplex() {
        return complex;
    }

    public AbstractSimpleDO getSimple() {
        return simple;
    }

    public MyArrayList<AbstractItemDO> getItems() {
        return items;
    }

    public List<String> getSimpleItems() {
        return simpleItems;
    }

    public String[] getArrayItems() {
        return arrayItems;
    }
}
